package util

import "time"

const LayoutISO = "2006-01-02"
const LayoutMinute = "2006-01-02 15:04"
const LayoutSecond = "2006-01-02 15:04:05"
const LayoutSecondWithTimeZone = "2006-01-02 15:04:05 -0700"
const LayoutSecondWithTimeZoneAndT = "2006-01-02T15:04:05-07:00"
const LayoutyyMMddHHmmss = "060102150405"
const LayoutYYMMdd = "20060102"

func getCSTTimeZone() *time.Location {
	return time.FixedZone("CST", 8*3600)
}

func GetNow() time.Time {
	return time.Now().In(getCSTTimeZone())
}

func TimePtrToString(timePtr *time.Time, layout string) string {
	if timePtr == nil {
		return ""
	}
	return timePtr.Format(layout)
}

func ParseTimePtr(layout string, timeStr string) (*time.Time, error) {
	var r *time.Time
	if timeStr != "" {
		t, err := time.ParseInLocation(layout, timeStr, getCSTTimeZone())
		if err != nil {
			return nil, err
		}
		r = &t
	}
	return r, nil
}

func ParseTime(layout string, timeStr string) (time.Time, error) {
	var r time.Time
	if timeStr != "" {
		t, err := time.ParseInLocation(layout, timeStr, getCSTTimeZone())
		if err != nil {
			return r, err
		}
		r = t
	}
	return r, nil
}
